package net.lab1024.sa.admin.module.business.research.service;

import java.util.List;
import net.lab1024.sa.admin.module.business.research.dao.ResearchDao;
import net.lab1024.sa.admin.module.business.research.domain.entity.ResearchEntity;
import net.lab1024.sa.admin.module.business.research.domain.form.ResearchAddForm;
import net.lab1024.sa.admin.module.business.research.domain.form.ResearchQueryForm;
import net.lab1024.sa.admin.module.business.research.domain.form.ResearchUpdateForm;
import net.lab1024.sa.admin.module.business.research.domain.vo.ResearchVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 科学论文 Service
 *
 * @Author KeissCake
 * @Date 2024-09-18 21:41:14
 * @Copyright 暂无
 */

@Service
public class ResearchService {

    @Resource
    private ResearchDao researchDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<ResearchVO> queryPage(ResearchQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ResearchVO> list = researchDao.queryPage(page, queryForm);
        PageResult<ResearchVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(ResearchAddForm addForm) {
        ResearchEntity researchEntity = SmartBeanUtil.copy(addForm, ResearchEntity.class);
        researchDao.insert(researchEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(ResearchUpdateForm updateForm) {
        ResearchEntity researchEntity = SmartBeanUtil.copy(updateForm, ResearchEntity.class);
        researchDao.updateById(researchEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        researchDao.batchUpdateDeleted(idList, true);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer researchId) {
        if (null == researchId){
            return ResponseDTO.ok();
        }

        researchDao.updateDeleted(researchId, true);
        return ResponseDTO.ok();
    }
}
